package de.baumann.browser.views.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.Html;
import android.widget.Button;
import android.widget.TextView;

import de.baumann.browser.R;

/**
 * @author:
 * @date:2019/7/10 15:43
 * @discription: 收金币
 */
@Deprecated
public class GatherGlodDialog extends BaseDialog {

    private TextView tvAdTitle;
    private Button btnObtain;
    private OnElementClickListener elementClickListener;
    private int times = 3;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            times--;
            if (times > 0) {
                btnObtain.setText(String.format(getContext().getString(R.string.charge_immediately), times));
                sendEmptyMessageDelayed(1, 1000);
            } else {
                btnObtain.setEnabled(true);
                btnObtain.setText(getContext().getString(R.string.get_it_right_now));
            }
        }
    };

    @SuppressLint("StringFormatInvalid")
    public GatherGlodDialog(@NonNull Context context, String odin, OnElementClickListener elementClickListener) {
        super(context);
        this.elementClickListener = elementClickListener;
        tvAdTitle.setText(Html.fromHtml(context.getString(R.string.ad)));
        Resources resources = context.getResources();
        ((TextView) findViewById(R.id.tvAdOdin)).setText(String.format(resources.getString(R.string.rv_odin), odin));
        btnObtain.setText(String.format(getContext().getString(R.string.charge_immediately), times));
        handler.sendEmptyMessageDelayed(1, 1000);
    }

    @Override
    protected int laoutId() {
        return R.layout.dialog_show_ad;
    }

    @Override
    protected void initView() {
        setCanceledOnTouchOutside(false);
        btnObtain = findViewById(R.id.btnObtain);
        btnObtain.setOnClickListener(v -> {
            dismiss();
            elementClickListener.clickButton();
        });
        tvAdTitle = findViewById(R.id.tvAdTitle);
//        loadAd();
    }


    public interface OnElementClickListener {
        void clickButton();
    }
}
